package communication;

import common.Discussion;
import common.DiscussionSentence;

public class HtmlMessageFormat implements IMessageFormat {

	
	private Discussion discussion;
	
	public HtmlMessageFormat(Discussion discussion) {
		super();
		this.discussion = discussion;
	}

	public Discussion getDiscussion() {
		return discussion;
	}

	public void setDiscussion(Discussion discussion) {
		this.discussion = discussion;
	}

	public String getHeader() {
		return header;
	}

	public String getFooter() {
		return footer;
	}

	//....................................................................
	//debut modification pour l'ajout de la génération du code HTML
	//les composants du code HTML à générer
	private final String header = Messages.getString("HtmlMessageFormat.0") + //$NON-NLS-1$
	Messages.getString("HtmlMessageFormat.1") + //$NON-NLS-1$
	Messages.getString("HtmlMessageFormat.2"); //$NON-NLS-1$
	private final String footer = Messages.getString("HtmlMessageFormat.3") ; //$NON-NLS-1$
	private String body;
	
	
	
	
	@Override
	public Object getMessageFormat(Discussion discussion) {
		
			body = "";
			for (DiscussionSentence sentence : discussion.getSentences()) {
				body+=FormateLine(sentence);
			}
		
			return(header + body + footer);	
	
	}
	
	// insérer la nouvelle ligne dans des balises HTML
	private String FormateLine(DiscussionSentence line){
		String htmlLine;
		String locutorName = ""; 
		locutorName = line.getLocutor().getShowName();

		switch (line.getMessageType()) {
		case NORMAL:
			htmlLine = "<TR><TD>";
				htmlLine += "<b>" +locutorName + "</b>";
			
			if (line.getColor()!= "") //$NON-NLS-1$
			 htmlLine +=	"<font color=" + line.getColor() +">"; 
			htmlLine += "\""+ line.getContent() + "</TD></TR>\n";
			return htmlLine;
		case INFO:
			return "";
		case NOTIFICATION:
			//newLine.getDiscussion().clearNotifications();
			return "<TR><TD> "+ locutorName + "is typing" + "</TD></TR>\n";
		}
		return null;
		
	}

	@Override
	public Object addSentence(DiscussionSentence sentence) {
		body += FormateLine(sentence);
		return null;
	}

}
